/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__sip_packet_check_fragment.c                    */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/16 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/sip/config.h"
#include "../test_sip_comm.h"
#include <sip/sip_core.h>
#include <sip/sip_header.h>
#include <sip/sip_comm.h>
#include <sip/sip_packet.h>
#include <frct_comm.h>

extern void __sip_destroy_packet(L7_sip_packet **pp_st_sip_packet);
extern int __sip_packet_check_fragment(int i_is_request
                                , char *p_payload
                                , unsigned int ui_payload_len
                                , struct timeval st_timestamp
                                , L7_sip_packet *p_st_sip_packet);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    L7_sip_packet *p_st_sip_packet = NULL;
    p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_sip_packet);
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));

    CU_ASSERT_EQUAL(sip_init_packet(p_st_sip_packet), FR_SUCC);

    char *p_payload = (char *) "INVITE sip:42010606001320082255_1@10.72.1.125 SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 10.72.8.120:5000;rport;branch=z9hG4bK1099270289\r\n"
            "From: <sip:42000000004002685574@10.72.8.120>;tag=4137830241\r\n"
            "To: <sip:42010606001320082255_1@10.72.1.125>\r\n"
            "Call-ID: 3321817563@10.72.8.120\r\n"
            "CSeq: 20 INVITE\r\n"
            "Contact: <sip:42000000004002685574@10.72.8.120:5000>\r\n"
            "Max-Forwards: 5\r\n"
            "User-Agent: mediasip/2.0\r\n"
            "Expires: 120\r\n"
            "Subject: 42010606001320082255:1,42000000004002685574:1\r\n"
            "Allow: INVITE, ACK, UPDATE, INFO, CANCEL, BYE, OPTIONS, REFER, SUBSCRIBE, NOTIFY, MESSAGE\r\n"
            "Content-Type: application/sdp\r\n"
            "Content-Length:   276\r\n"
            "\r\n"
            "v=0\r\n"
            "o=42000000004002685574 123456 0 IN IP4 10.72.8.120\r\n"
            "s=Play\r\n"
            "c=IN IP4 10.72.8.120\r\n"
            "t=0 0\r\n"
            "m=video 20000 RTP/AVP 96 98 97 109\r\n"
            "a=rtpmap:96 PS/90000\r\n"
            "a=rtpmap:98 H264/90000\r\n"
            "a=rtpmap:97 MPEG4/90000\r\n"
            "a=rtpmap:109 HXHT/90000\r\n"
            "a=recvonly\r\n"
            "y=0999999999\r\n"
            "f=v/2/5/25/1/2a/8/4/16\r\n";

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;

    CU_ASSERT_EQUAL(__sip_packet_check_fragment(FR_YES, p_payload, strlen(p_payload), st_timestamp, p_st_sip_packet), APM_SIP_CHECK_FRAGMENT_SUCCESS);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

static void __case2(void)
{
    L7_sip_packet *p_st_sip_packet = NULL;
    p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_sip_packet);
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));

    CU_ASSERT_EQUAL(sip_init_packet(p_st_sip_packet), FR_SUCC);

    char *p_payload = (char *) "SIP/2.0 200 OK\r\n"
            "Via: SIP/2.0/UDP 10.72.8.120:5000;rport=5000;branch=z9hG4bK1099270289\r\n"
            "From: <sip:42000000004002685574@10.72.8.120>;tag=4137830241\r\n"
            "To: <sip:42010606001320082255_1@10.72.1.125>;tag=982099237\r\n"
            "Call-ID: 3321817563@10.72.8.120\r\n"
            "CSeq: 20 INVITE\r\n"
            "Contact: <sip:42010606001320082255_1@10.72.1.125:5060>\r\n"
            "Allow: INVITE, ACK, OPTIONS, CANCEL, BYE, SUBSCRIBE, NOTIFY, MESSAGE, INFO, REFER, UPDATE\r\n"
            "Content-Type: application/sdp\r\n"
            "Content-Length:   188\r\n"
            "\r\n"
            "v=0\r\n"
            "o=42010000002004503598 123456 0 IN IP4 10.72.38.12\r\n"
            "s=Play\r\n"
            "c=IN IP4 10.72.38.12\r\n"
            "t=0 0\r\n"
            "m=video 26300 RTP/AVP 96\r\n"
            "a=rtpmap:96 PS/90000\r\n"
            "a=sendonly\r\n"
            "y=0000002461\r\n"
            "f=v/2/5/25/1/2a///\r\n";

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;

    CU_ASSERT_EQUAL(__sip_packet_check_fragment(FR_YES, p_payload, strlen(p_payload), st_timestamp, p_st_sip_packet), APM_SIP_CHECK_FRAGMENT_SUCCESS);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

static void __case3(void)
{
    L7_sip_packet *p_st_sip_packet = NULL;
    p_st_sip_packet = (L7_sip_packet *) frmalloc(sizeof(L7_sip_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_sip_packet);
    memset(p_st_sip_packet, 0x00, sizeof(L7_sip_packet));

    CU_ASSERT_EQUAL(sip_init_packet(p_st_sip_packet), FR_SUCC);

    char *p_payload = (char *) "INVITE sip:42010606001320082255_1@10.72.1.125 SIP/2.0\r\n"
            "Via: SIP/2.0/UDP 10.72.8.120:5000;rport;branch=z9hG4bK1099270289\r\n"
            "From: <sip:42000000004002685574@10.72.8.120>;tag=4137830241\r\n"
            "To: <sip:42010606001320082255_1@10.72.1.125>\r\n"
            "Call-ID: 3321817563@10.72.8.120\r\n"
            "CSeq: 20 INVITE\r\n"
            "Contact: <sip:42000000004002685574@10.72.8.120:5000>\r\n"
            "Max-Forwards: 5\r\n"
            "User-Agent: mediasip/2.0\r\n"
            "Expires: 120\r\n"
            "Subject: 42010606001320082255:1,42000000004002685574:1\r\n"
            "Allow: INVITE, ACK, UPDATE, INFO, CANCEL, BYE, OPTIONS, REFER, SUBSCRIBE, NOTIFY, MESSAGE\r\n"
            "Content-Type: application/sdp\r\n"
            "Content-Length:   276\r\n"
            "\r\n"
            "v=0\r\n"
            "o=42000000004002685574 123456 0 IN IP4 10.72.8.120\r\n"
            "s=Play\r\n"
            "c=IN IP4 10.72.8.120\r\n"
            "t=0 0\r\n"
            "m=video 20000 RTP/AVP 96 98 97 109\r\n"
            "a=rtpmap:96 PS/90000\r\n"
            "a=rtpmap:98 H264/90000\r\n"
            "a=rtpmap:97 MPEG4/90000\r\n"
            "a=rtpmap:109 HXHT/90000\r\n"
            "y=0999999999\r\n"
            "f=v/2/5/25/1/2a/8/4/16\r\n";

    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;

    CU_ASSERT_EQUAL(__sip_packet_check_fragment(FR_YES, p_payload, strlen(p_payload), st_timestamp, p_st_sip_packet), APM_SIP_CHECK_FRAGMENT_NEED_NEXT_PACKET);

    __sip_destroy_packet(&p_st_sip_packet);
    CU_ASSERT_PTR_NULL(p_st_sip_packet);
}

int add_suite__sip_packet_check_fragment(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __sip_packet_check_fragment()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
